﻿@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication

@inject IAccessTokenProvider TokenProvider
@inject NavigationManager Navigation
@inject SignOutSessionStateManager SignOutManager
@inject IJSRuntime JSRuntime
@inject Blazor.Modules.Core.Services.ITokenManager tokenManager

<div class="top-row pl-4 navbar navbar-dark">
    <a class="navbar-brand" href="">Blazor WebAssembly</a>
    <button class="navbar-toggler" @onclick="ToggleNavMenu">
        <span class="navbar-toggler-icon"></span>
    </button>
</div>

<div class="@NavMenuCssClass" @onclick="ToggleNavMenu">
    <ul class="nav flex-column">
        <li class="nav-item px-3">
            <NavLink class="nav-link" href="" Match="NavLinkMatch.All">
                <span class="oi oi-home" aria-hidden="true"></span> Home
            </NavLink>
        </li>
        <li class="nav-item px-3">
            <NavLink class="nav-link" href="files">
                <span class="oi oi-document" aria-hidden="true"></span> Files
            </NavLink>
        </li>
        <li class="nav-item px-3">
            <NavLink class="nav-link" href="products">
                <span class="oi oi-list-rich" aria-hidden="true"></span> Products
            </NavLink>
        </li>
        <li class="nav-item px-3">
            <NavLink class="nav-link" href="users">
                <span class="oi oi-people" aria-hidden="true"></span> Users
            </NavLink>
        </li>
        <li class="nav-item px-3">
            <NavLink class="nav-link" href="auditlogs">
                <span class="oi oi-dashboard" aria-hidden="true"></span> Audit Logs
            </NavLink>
        </li>
        <AuthorizeView>
            <Authorized>
                <li class="nav-item px-3">
                    <a class="nav-link" @onclick="SignOut" href="javascript:void(0)">
                        <span class="oi oi-account-logout" aria-hidden="true"></span> Logout
                    </a>
                </li>
            </Authorized>
            <NotAuthorized>
                <li class="nav-item px-3">
                    <NavLink class="nav-link" href="/authentication/login">
                        <span class="oi oi-account-login" aria-hidden="true"></span> Login
                    </NavLink>
                </li>
            </NotAuthorized>
        </AuthorizeView>
    </ul>
</div>

@code {
    private bool collapseNavMenu = true;

    private string NavMenuCssClass => collapseNavMenu ? "collapse" : null;

    private void ToggleNavMenu()
    {
        collapseNavMenu = !collapseNavMenu;
    }

    private async Task SignOut(MouseEventArgs args)
    {
        //var tokenResult = await TokenProvider.RequestAccessToken();
        //if (tokenResult.TryGetToken(out var token))
        //{
        //    await JSRuntime.InvokeVoidAsync("alert", token.Value);
        //}

        //var token = await tokenManager.GetToken();
        //await JSRuntime.InvokeVoidAsync("alert", token.AccessToken);
        //await JSRuntime.InvokeVoidAsync("alert", token.ExpiresAt.ToString());

        await SignOutManager.SetSignOutState();
        Navigation.NavigateTo("authentication/logout");
    }
}
